package edu.friday.controller;


import edu.friday.common.base.BaseController;
import edu.friday.common.result.RestResult;
import edu.friday.common.result.TableDataInfo;
import edu.friday.common.constant.UserConstants;
import edu.friday.model.vo.SysUserVo;
import edu.friday.service.SysRoleService;
import edu.friday.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController {

    @Autowired
    SysUserService userService;

    @Autowired
    SysRoleService roleService;

    @GetMapping(value = {"/","/{userId}"})
    public RestResult getInfo(@PathVariable(value = "userId",required = false) Long userId) {
        RestResult result = RestResult.success();
        result.put("roles",roleService.selectRoleAll());
        if (userId != null) {
            result.put(RestResult.DATA_TAG,userService.selectUserById(userId));
            result.put("roleIds",roleService.selectRoleListByUserId(userId));
            return result;
        }
        return result;
    }


    @GetMapping("/list")

//    }

    public TableDataInfo list(SysUserVo user,@PageableDefault Pageable pageable) {
        int pageNum = pageable.getPageNumber() -1;
        pageNum = Math.max(pageNum,0);
        pageable = PageRequest.of(pageNum,pageable.getPageSize());
        return userService.selectUserByPage(user,pageable);
    }


    @PostMapping
    public RestResult add(@RequestBody @Validated SysUserVo user) {
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName()))){
            return RestResult.error("新增用户"+user.getUserName()+"失败，账号已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(userService.checkUserPhoneUnique(user))) {
            return RestResult.error("新增用户"+user.getPhonenumber()+"失败，手机号码已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
            return RestResult.error("新增用户"+user.getEmail()+"失败，邮箱已存在");
        }
        user.setCreateBy("system");
        return toAjax(userService.insertUser(user));
    }


    @DeleteMapping("/{userIds}")
    public RestResult remove(@PathVariable Long [] userIds) {
//        return toAjax(userService.deleteUserByIds(userIds));
        return toAjax(userService.realDeleteUserByIds(userIds));
    }

    @PutMapping
    public RestResult update(@RequestBody @Validated SysUserVo user) {
        String userName=user.getUserName();
        user.setUpdateBy(null);
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserPhoneUnique(user))) {
            return RestResult.error("修改用户"+userName+"失败，手机号码已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user))) {
            return RestResult.error("修改用户"+userName+"失败，邮箱已存在");
        }
        user.setUpdateBy("system");
        return toAjax(userService.updateUser(user));
    }
}
